home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / amiga / gui / x / xfig.lha / src / x11 / e_align.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-26  |  4.9 KB  |  226 lines

  1. /*
  2.  * FIG : Facility for Interactive Generation of figures
  3.  * Copyright (c) 1985 by Supoj Sutanthavibul
  4.  *
  5.  * "Permission to use, copy, modify, distribute, and sell this software and its
  6.  * documentation for any purpose is hereby granted without fee, provided that
  7.  * the above copyright notice appear in all copies and that both the copyright
  8.  * notice and this permission notice appear in supporting documentation. 
  9.  * No representations are made about the suitability of this software for 
  10.  * any purpose.  It is provided "as is" without express or implied warranty."
  11.  */
  12.  
  13. #include "fig.h"
  14. #include "resources.h"
  15. #include "object.h"
  16. #include "paintop.h"
  17. #include "mode.h"
  18. #include "u_create.h"
  19. #include "u_draw.h"
  20. #include "u_search.h"
  21. #include "u_undo.h"
  22. #include "w_canvas.h"
  23. #include "w_mousefun.h"
  24. #include "w_setup.h"
  25.  
  26. static int    init_align(), init_align_canvas();
  27. static int    llx, lly, urx, ury;
  28. static int    xcmin, ycmin, xcmax, ycmax;
  29. static int    dx, dy;
  30. static int    align_arc();
  31. static int    align_ellipse();
  32. static int    align_line();
  33. static int    align_spline();
  34. static int    align_text();
  35. static int    align_compound();
  36. static int    get_dx_dy();
  37.  
  38. align_selected()
  39. {
  40.     set_mousefun("align compound", "align canvas", "");
  41.     canvas_kbd_proc = null_proc;
  42.     canvas_locmove_proc = null_proc;
  43.     init_searchproc_left(init_align);
  44.     canvas_leftbut_proc = object_search_left;
  45.     canvas_middlebut_proc = init_align_canvas;
  46.     canvas_rightbut_proc = null_proc;
  47.     set_cursor(pick15_cursor);
  48. }
  49.  
  50. /* align objects to the whole canvas */
  51.  
  52. static
  53. init_align_canvas(x, y, shift)
  54.     int            x, y;
  55.     unsigned int    shift;    /* Shift Key Status from XEvent */
  56. {
  57.     cur_c = &objects;
  58.     toggle_all_compoundmarkers();
  59.     draw_compoundelements(cur_c, ERASE);
  60.     old_c = copy_compound(&objects);
  61.     xcmin=ycmin=0;
  62.     if (appres.INCHES)
  63.     {
  64.     xcmax=(appres.landscape? 11*PIX_PER_INCH : 8.5*PIX_PER_INCH);
  65.     ycmax=(appres.landscape? 8.5*PIX_PER_INCH : 11*PIX_PER_INCH);
  66.     }
  67.     else
  68.     {
  69.     xcmax=(appres.landscape? 29.7*PIX_PER_CM : 21*PIX_PER_CM);
  70.     ycmax=(appres.landscape? 21*PIX_PER_CM : 29.7*PIX_PER_CM);
  71.     }
  72.     align_ellipse();
  73.     align_arc();
  74.     align_line();
  75.     align_spline();
  76.     align_compound();
  77.     align_text();
  78.     draw_compoundelements(cur_c, PAINT);
  79.     toggle_all_compoundmarkers();
  80.     clean_up();
  81.     set_latestobjects(old_c);
  82.     set_action_object(F_CHANGE, O_ALL_OBJECT);
  83.     set_modifiedflag();
  84. }
  85.  
  86. static
  87. init_align(p, type, x, y, px, py)
  88.     char       *p;
  89.     int            type;
  90.     int            x, y;
  91.     int            px, py;
  92. {
  93.     if (type != O_COMPOUND)
  94.     return;
  95.     cur_c = (F_compound *) p;
  96.     toggle_compoundmarker(cur_c);
  97.     draw_compoundelements(cur_c, ERASE);
  98.     old_c = copy_compound(cur_c);
  99.     compound_bound(cur_c, &xcmin, &ycmin, &xcmax, &ycmax);
  100.     align_ellipse();
  101.     align_arc();
  102.     align_line();
  103.     align_spline();
  104.     align_compound();
  105.     align_text();
  106.     /*
  107.      * recompute the compound's bounding box
  108.      */
  109.     compound_bound(cur_c, &cur_c->nwcorner.x, &cur_c->nwcorner.y,
  110.            &cur_c->secorner.x, &cur_c->secorner.y);
  111.     draw_compoundelements(cur_c, PAINT);
  112.     toggle_compoundmarker(cur_c);
  113.     clean_up();
  114.     old_c->next = cur_c;
  115.     set_latestcompound(old_c);
  116.     set_action_object(F_CHANGE, O_COMPOUND);
  117.     set_modifiedflag();
  118. }
  119.  
  120. static int
  121. align_ellipse()
  122. {
  123.     F_ellipse       *e;
  124.  
  125.     for (e = cur_c->ellipses; e != NULL; e = e->next) {
  126.     ellipse_bound(e, &llx, &lly, &urx, &ury);
  127.     get_dx_dy();
  128.     translate_ellipse(e, dx, dy);
  129.     }
  130. }
  131.  
  132. static int
  133. align_arc()
  134. {
  135.     F_arc       *a;
  136.  
  137.     for (a = cur_c->arcs; a != NULL; a = a->next) {
  138.     arc_bound(a, &llx, &lly, &urx, &ury);
  139.     get_dx_dy();
  140.     translate_arc(a, dx, dy);
  141.     }
  142. }
  143.  
  144. static int
  145. align_line()
  146. {
  147.     F_line       *l;
  148.  
  149.     for (l = cur_c->lines; l != NULL; l = l->next) {
  150.     line_bound(l, &llx, &lly, &urx, &ury);
  151.     get_dx_dy();
  152.     translate_line(l, dx, dy);
  153.     }
  154. }
  155.  
  156. static int
  157. align_spline()
  158. {
  159.     F_spline       *s;
  160.  
  161.     for (s = cur_c->splines; s != NULL; s = s->next) {
  162.     spline_bound(s, &llx, &lly, &urx, &ury);
  163.     get_dx_dy();
  164.     translate_spline(s, dx, dy);
  165.     }
  166. }
  167.  
  168. static int
  169. align_compound()
  170. {
  171.     F_compound       *c;
  172.  
  173.     for (c = cur_c->compounds; c != NULL; c = c->next) {
  174.     compound_bound(c, &llx, &lly, &urx, &ury);
  175.     get_dx_dy();
  176.     translate_compound(c, dx, dy);
  177.     }
  178. }
  179.  
  180. static int
  181. align_text()
  182. {
  183.     F_text       *t;
  184.  
  185.     for (t = cur_c->texts; t != NULL; t = t->next) {
  186.     int   dum;
  187.     text_bound_actual(t, t->angle, &llx, &lly, &urx, &ury,
  188.            &dum,&dum,&dum,&dum,&dum,&dum,&dum,&dum);
  189.     get_dx_dy();
  190.     translate_text(t, dx, dy);
  191.     }
  192. }
  193.  
  194. static int
  195. get_dx_dy()
  196. {
  197.     switch (cur_valign) {
  198.     case NONE:
  199.     dy = 0;
  200.     break;
  201.     case TOP:
  202.     dy = ycmin - lly;
  203.     break;
  204.     case BOTTOM:
  205.     dy = ycmax - ury;
  206.     break;
  207.     case CENTER:
  208.     dy = (ycmin - lly) + (abs(ycmin - lly) + abs(ycmax - ury)) / 2;
  209.     break;
  210.     }
  211.     switch (cur_halign) {
  212.     case NONE:
  213.     dx = 0;
  214.     break;
  215.     case LEFT:
  216.     dx = xcmin - llx;
  217.     break;
  218.     case RIGHT:
  219.     dx = xcmax - urx;
  220.     break;
  221.     case CENTER:
  222.     dx = (xcmin - llx) + (abs(xcmin - llx) + abs(xcmax - urx)) / 2;
  223.     break;
  224.     }
  225. }
  226.